﻿Public Class Campeonatos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            PreencheDados()

        End If

    End Sub

    Private Sub PreencheDados()

        Dim BL As New BusinessLogic.BLApostas.BLApostas

        grdCampeonatos.DataSource = BL.ObterUtilizadorCampeonatos(CInt(Session("IDUtilizador").ToString))
        grdCampeonatos.DataBind()

    End Sub

    Protected Sub grdCampeonatos_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCampeonatos.RowCommand

        If e.CommandName.Equals("Activar") Then

            Dim row As GridViewRow = DirectCast((DirectCast(e.CommandSource, ImageButton).NamingContainer), GridViewRow)
            Dim Activo As String = DirectCast(row.FindControl("lblActivo"), Label).Text

            If Activo.Equals("True") Then

                'ScriptManager.RegisterStartupScript(Me, GetType(Page), "mensagem", "alert('Campeonato já se encontra Activo!');", True)

            ElseIf Activo.Equals("False") Then

                Dim BL As New BusinessLogic.BLApostas.BLApostas

                Dim resultado As String = BL.ActivarCampeonato(CInt(Session("IDUtilizador").ToString), e.CommandArgument)

                If Not resultado.Equals("OK") Then

                    'ScriptManager.RegisterStartupScript(Me, GetType(Page), "mensagem", "alert('" & resultado & "');", True)
                End If

                PreencheDados()

            End If

        End If

    End Sub

    Protected Sub grdCampeonatos_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCampeonatos.RowDataBound

        If e.Row.RowType.Equals(DataControlRowType.DataRow) Then

            Dim Activo As String = CType(e.Row.FindControl("lblActivo"), Label).Text

            If Activo.Equals("True") Then

                Dim img As ImageButton = CType(e.Row.FindControl("imgActivar"), ImageButton)
                img.ImageUrl = "~/Styles/Images/smiley-mr-green.png"

            ElseIf Activo.Equals("False") Then

                Dim img As ImageButton = CType(e.Row.FindControl("imgActivar"), ImageButton)
                img.ImageUrl = "~/Styles/Images/smiley-mad.png"

            End If

        End If

    End Sub

End Class